/*
 * 页面功能
 * @Author: kaito 
 * @Date: 2018-03-26 10:02:21 
 * @Last Modified by: kaito
 * @Last Modified time: 2018-03-28 08:43:03
 */

const uRls = {
    "user": '',
    "admin": '',
    "order": '',
    'product':'',
    'member':'',
    'qiniu':'' ,                    //获取七牛sdk秘钥
    'hotCB':'',
    'brandB':''
}
/*
------------------------------------------------user 部分-------------------------------------------------------------------------

    规定传入admin格式 admin={admId:'', admToken:''}
*/
function __user() {
    // 获取用户/用户列表
    this.get = function (admin, page = None, userId = None, callback, errorback) {
        //可选参数 --page (分页页码) --userId（用户id 获取一个用户）
        if (!page) page = 1;
        if (userId) {
            getOne();
        } else {
            getList();
        }

        function getOne() {
            axios({
                method: 'GET',
                url: uRls.user,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    userId: userId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
        }
        function getList() {
            axios({
                method: 'GET',
                url: uRls.user,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    page: page
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
        }
    };
    //删除用户 ---谨慎使用！！！！  --需要超级管理员权限
    this.delete = function (admin, userId, callback, errorback) {
        if (!admin || !userId) return false;
        axios({
            method: 'DELETE',
            url: uRls.user,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                userId: userId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        })
    };
    //添加用户  ---谨慎使用！！！！  --需要超级管理员权限
    this.add = function (admin, userId, callback, errorback) {
        if (!admin || !userId) return false;
        axios({
            method: 'PUT',
            url: uRls.user,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                userId: userId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(errorback)
        })
    }
    //修改用户信息 ---谨慎使用！！！！  --需要超级管理员权限
    this.updata = function (admin, userId, info ,callback, errorback) {
        if (!admin || !userId) return false;
        axios({
            method: 'POST',
            url: uRls.user,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                userId: userId,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        })
    };

    return this;
}

/*
------------------------------------------------order 部分------------------------------------------------------------------------
*/
function __order() {
    // get 获取订单列表、订单详情
    this.get = function (admin, page = None, orderId = None, callback, errorback) {
        if (!admin) return false;
        if (!page) page = 1;
        if (orderId){
            _detail()
        }else{
            _list()
        }
        function _list() { 
            axios({
                method: 'GET',
                url: uRls.order,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    page: page
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            })
         };
        function _detail() { 
            axios({
                method: 'GET',
                url: uRls.order,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    orderId: orderId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            })
         }; 

    }
    // post 修改订单 --需超级管理员权限
    this.updata = function (admin, orderId, info, callback, errorback) { 
        if (!admin || !orderId || !info) return false;
        axios({
            method: 'POST',
            url: uRls.order,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                orderId: orderId,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        })
     };
     //put 新增订单 ----不提供
     this.add = function () { 
         console.log('不提供本方法');
         return false;
      }

     //delete 删除订单 --需超级管理员权限
    this.delete = function (admin, orderId, callback, errorback) {  
        if (!admin || !orderId) return false;
        axios({
            method: 'DELETE',
            url: uRls.order,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                orderId: orderId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        })
    }

    return this;   
}

/*
------------------------------------------------news 部分------------------------------------------------------------------------
*/
function __news() { 
    //get 获取新闻列表/新闻详情
    this.get = function (admin, page = None, newsId = None, callback, errorback) {  
        if(!admin) return false;
        if(!page) page = 1;
        if(newsId){ _detail() } else{ _list() };
    
        function _detail() { 
            axios({
                method: 'GET',
                url: uRls.news,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    newsId: newsId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         }
        function _list() { 
            axios({
                method: 'GET',
                url: uRls.news,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    page: page
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         };
    };
    //post 修改新闻详情
    this.updata = function (admin, newsId, callback, errorback) { 
        if (!admin || !newsId) return false;
        axios({
            method: 'POST',
            url: uRls.news,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                newsId: newsId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };
     //delete 删除新闻条目
    this.delete = function (admin, newsId, callback, errorback) { 
        if (!admin || !newsId) return false;
        axios({
            method: 'DELETE',
            url: uRls.news,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                newsId: newsId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };
     //put --新增新闻内容
    this.add = function (admin, info, callback, errorback) {  
        if (!admin || !info) return false;
        axios({
            method: 'DELETE',
            url: uRls.news,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    };

    return this;
 }

/* 
------------------------------------------------video 部分------------------------------------------------------------------------
*/
function __video() { 
    //get --获取video列表/详情
    this.get = function (admin, page=None, videoId=None, callback, errorback) { 
        if(!admin) return false;
        if(!page) page = 1;
        if (videoId) { _detail() } else { _list() }
        function _list() { 
            axios({
                method: 'GET',
                url: uRls.video,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    page: page
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         };
        function _detail() { 
            axios({
                method: 'GET',
                url: uRls.video,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    videoId: videoId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         };
     };
     // post --修改视频详情
    this.updata = function (admin, videoId, callback, errorback) { 
        if (!admin || !videoId) return false;
        axios({
            method: 'POST',
            url: uRls.video,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                videoId: videoId
            }
        }).then(function (response) {  
            callback(response)
            }).catch(function (error) {
                errorback(error)
            });
     }
     // put --新增视频内容
    this.add = function (admin, info, callback, errorback) { 
        if (!admin || !info) return false;
        axios({
            method: 'PUT',
            url: uRls.video,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    } 
    // delete --删除视频
    this.delete = function (admin, videoId, callback, errorback) { 
        if (!admin || !videoId) return false;
        axios({
            method: 'DELETE',
            url: uRls.video,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                videoId: videoId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };

     return this;
}

/* 
------------------------------------------------member 部分------------------------------------------------------------------------
*/
function __member() {
    //get --获取经销商列表/详情
    this.get = function (admin, memberId = None, page = None, callback, errorback) { 
        if(!admin) return false;
        if(!page) page = 1;
        if (memberId) { _detail() } else { _list() };
        function _list() { 
            axios({
                method: 'GET',
                url: uRls.member,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    memberId: memberId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         };
        function _detail() { 
            axios({
                method: 'GET',
                url: uRls.member,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    page: page
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         };
    }
    // post --修改经销商信息
    this.updata = function (admin, memberId, info,callback, errorback) { 
        if (!admin || !memberId) return false;
        axios({
            method: 'POST',
            url: uRls.member,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                memberId: memberId,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    };
    // put --新增经销商信息
    this.add = function (admin, info, callback, errorback) {  
        if(!admin || !info) return false;
        axios({
            method: 'PUT',
            url: uRls.member,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    }
    // delete --删除经销商信息
    this.delete = function (admin, memberId, callback, errorback) { 
        if (!admin || !memberId) return false;
        axios({
            method: 'DELETE',
            url: uRls.member,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                memberId: memberId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };
     
     return this;
}
/* 
------------------------------------------------product 部分------------------------------------------------------------------------
*/
function __product() { 
    //get --获取产品列表/详情信息
    this.get = function (admin, pId=None, page=None, callback, errorback) { 
        if(!admin) return false;
        if(!page) page = 1;
        if (pId) { _detail() } else { _list() }

        function _list() { 
            axios({
                method: 'GET',
                url: uRls.product,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    page: page
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         }
        function _detail() { 
            axios({
                method: 'GET',
                url: uRls.product,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    pId: pId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         }
     };
     //post --修改单个商品信息
    this.updata = function (admin, pId, info, callback, errorback) {
        if (!admin || !pId || !info) return false;
        axios({
            method: 'POST',
            url: uRls.product,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                pId: pId,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    };
    //put --新增单个商品信息
    this.add = function (admin, info, callback, errorback) { 
        if (!admin || !info) return false;
        axios({
            method: 'PUT',
            url: uRls.product,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };
    // delete --删除单个商品信息
    this.delete = function (admin, pId, callback, errorback) { 
        if (!admin || !pId) return false;
        axios({
            method: 'DELETE',
            url: uRls.product,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                pId: pId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };

     return this;
 };
/* 
------------------------------------------------顶部轮播图 视频轮播------------------------------------------------------------------------
*/
function __banner() { 
    //get --获取轮播列表/轮播内容
    this.get = function (admin, bannerId=None, callback, errorback){
        if(!admin) return false;
        if (bannerId) { _detail()  } else { _list() }
        function _list() { 
            axios({
                method: 'GET',
                url: uRls.banner,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
        };
        function _detail() {
            axios({
                method: 'GET',
                url: uRls.banner,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    bannerId: bannerId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            }); 
        };
    };
    //post --修改单个轮播内容
    this.updata = function (admin, bannerId, info, callback, errorback) { 
        if (!admin || !bannerId || !info) return false;
        axios({
            method: 'POST',
            url: uRls.banner,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                bannerId: bannerId,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    }
    //put --新增轮播内容
    this.add = function (admin, info, callback, errorback) { 
        if (!admin || !info) return false;
        axios({
            method: 'PUT',
            url: uRls.banner,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };
    // delete --删除轮播内容
    this.delete = function (admin, bannerId, callback, errorback) { 
        if (!admin || !bannerId) return false;
        axios({
            method: 'DELETE',
            url: uRls.banner,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                bannerId: bannerId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    }; 

    return this;
 }
/* 
------------------------------------------------web 功能部分------------------------------------------------------------------------
*/
//第三方 -- 七牛云功能 uRls.qiniu
function __qiniu() {  //前端只提供图片选区 图片上传 图片删除功能
    var SDK = {}
    (() => {
        axios({
            method: 'POST',
            url: uRls.qiniu
        }).then(function (response) {
            if (response){
                SDK.ACCESS_KEY = response.data.ACCESS_KEY;
                SDK.SECRET_KEY = response.data.SECRET_KEY;
            }
        }).catch(function (error) {
            throw('call api server error for qiuniu SDK');
        });
    })
    function _img() { 
        //上传
        this.upload = function (admin) {  }
        //删除
        this.delete = function (admin) {  }
        return this;
     }
    function _video() { 
        //上传
        this.upload = function (admin) { }
        //删除
        this.delete = function (admin) { }
        return this;
     } 

    //构造
    this.img = function () { return new _img() }
    this.video = function () { return new _video()  }
    return this; 
 }
 
//热推轮播图
function __hotComB(params) {
    //get -- 获取轮播列表/轮播详情
    this.get = function (admin, pId=None, callback, errorback) { 
        if(!admin) return false;
        if (pId) { _detail()} else { _list() }
        function _list() { 
            axios({
                method: 'GET',
                url: uRls.hotCB,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
        }
        function _detail() { 
            axios({
                method: 'GET',
                url: uRls.hotCB,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    pId: pId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         }
     };
     //post --修改内容 --产品轮播 无此借口
     
     //put --新增轮播  最大轮播数量不超过12
    this.add = function (admin, pId, callback, errorback) { 
        if (!admin || !pId) return false;
        axios({
            method: 'PUT',
            url: uRls.hotCB,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                pId: pId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    };
    // delete 
    this.delete = function (admin, pId, callback, errorback) { 
        if (!admin || !pId) return false;
        axios({
            method: 'DELETE',
            url: uRls.hotCB,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                pId: pId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };

     return this;
}

//品宣轮播图
function _brandB() { 
    //get --获取列表/详情
    this.get = function (admin, brandBId=None, callback, errorback) { 
        if(!admin) return false;
        if (brandBId) { _detail() } else { _list() };
        function _list() { 
            axios({
                method: 'GET',
                url: uRls.brandB,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
        };
        function _detail() { 
            axios({
                method: 'GET',
                url: uRls.brandB,
                data: {
                    admId: admin.admId,
                    admToken: admin.admToken,
                    brandBId: brandBId
                }
            }).then(function (response) {
                callback(response)
            }).catch(function (error) {
                errorback(error)
            });
         }
     };
     //post 修改轮播内容
    this.updata = function (admin, brandBId, info ,callback, errorback) { 
        if (!admin || !brandBId) return false;
        axios({
            method: 'POST',
            url: uRls.brandB,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                brandBId: brandBId,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    };
    //put 新增品宣轮播  --最大轮播数不超过8
    this.add = function (admin, info, callback, errorback) { 
        if (!admin || !info) return false;
        axios({
            method: 'PUT',
            url: uRls.brandB,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                info: info
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
    }
    // delete --删除轮播内容
    this.delete = function (admin, brandBId, callback, errorback) { 
        if (!admin || !brandBId) return false;
        axios({
            method: 'DELETE',
            url: uRls.brandB,
            data: {
                admId: admin.admId,
                admToken: admin.admToken,
                brandBId: brandBId
            }
        }).then(function (response) {
            callback(response)
        }).catch(function (error) {
            errorback(error)
        });
     };

     return this;
 }


 
//构造
function User() {
    return new __user();
}
function Order() {  
    return new __order();
}
function News() {
    return new __news();
}
function Video() { 
    return new __video();
}
function Member() { 
    return new __member();
}
function Product() { 
    return new __product();
}
function Banner() {  
    return new __banner();
}
function Qiniu() {
    return new __qiniu();
}
function HotComB() { 
    return new __hotComB();
 }
function BrandB() { 
    return new _brandB()
 }








